#!/usr/bin/env bash
set -e

VERSION=${1:-3.8.0}

echo "Installing Python ${VERSION}..."

export PYENV_ROOT=~/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH

if [[ $VERSION != "system" ]]; then

  # Grab the Python version we want
  if ! [ -d $PYENV_ROOT/versions/$VERSION ]; then
    set -x
    mkdir -p $PYENV_ROOT/versions
    curl -sSL https://lambci.s3.amazonaws.com/binaries/python-${VERSION}.tgz | tar -xz -C $PYENV_ROOT/versions
    set +x
  fi

  # Unset this so we're not using the built-in path
  unset PYTHONPATH
fi

# Init pyenv
eval "$(pyenv init -)"
pyenv local $VERSION
python --version

echo "Python setup complete"
echo ""

set +e
